<?php 
function format_date($date_time, $format = "d/m/Y")
{
	return date($format, strtotime($date_time));
}

function format_date_international($date_time, $format = "%d/%B/%Y")
{
	switch(TRANSLATION_CODE)
	{
		case "fr":
			setlocale(LC_TIME, 'fr_FR@euro', 'fr_FR', 'fr');
		break;
		case "de":
			setlocale(LC_TIME, 'de_DE@euro', 'de_DE', 'de', 'ge');
		break;
	}
	return strftime($format, strtotime($date_time));
}

function format_db_insert_date($date_time)
{
	return date("Y-m-d", parse_date($date_time, "%d/%m/%Y"));
}

function format_db_insert_datetime($date_time)
{
	return date("Y-m-d H:i:s", parse_date($date_time, "%d/%m/%Y %H:%M:%p"));
}

function date_difference_in_days($start, $end) 
{   
	$start_ts = strtotime($start);   
	$end_ts = strtotime($end);   
	$diff = $end_ts - $start_ts;   
	return round($diff / 86400); 
} 

function parse_date($date, $format)
{
   // Builds up date pattern from the given $format, keeping delimiters in place.
   if( !preg_match_all( "/%([YmdHMp])([^%])*/", $format, $formatTokens, PREG_SET_ORDER ) ) {
       return false;
   }
   foreach( $formatTokens as $formatToken ) {
       $delimiter = preg_quote( $formatToken[2], "/" );
       $datePattern .= "(.*)".$delimiter;
   }
   
   // Splits up the given $date
   if( !preg_match( "/".$datePattern."/", $date, $dateTokens) ) {
       return false;
   }
   $dateSegments = array();
   for($i = 0; $i < count($formatTokens); $i++) {
       $dateSegments[$formatTokens[$i][1]] = $dateTokens[$i+1];
   }

   // Reformats the given $date into US English date format, suitable for strtotime()
   if( $dateSegments["Y"] && $dateSegments["m"] && $dateSegments["d"] ) {
       $dateReformated = $dateSegments["Y"]."-".$dateSegments["m"]."-".$dateSegments["d"];
   }
   else {
       return false;
   }
   if( $dateSegments["H"] && $dateSegments["M"] ) {
       $dateReformated .= " ".$dateSegments["H"].":".$dateSegments["M"];
   }
   
   return strtotime( $dateReformated );
}
?>